// MIT License

// Copyright (c) 2019 Erin Catto

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "test.h"

// This test shows how to apply forces and torques to a body.
// It also shows how to use the friction joint that can be useful
// for overhead games.
class ApplyForce : public Test
{
public:
    ApplyForce()
    {
        m_world->SetGravity(b2Vec2(0.0f, 0.0f));

        const float k_restitution = 0.4f;

        b2Body* ground;
        {
            b2BodyDef bd;
            bd.position.Set(0.0f, 20.0f);
            ground = m_world->CreateBody(&bd);

            b2EdgeShape shape;

            b2FixtureDef sd;
            sd.shape       = &shape;
            sd.density     = 0.0f;
            sd.restitution = k_restitution;

            // Left vertical
            shape.SetTwoSided(b2Vec2(-20.0f, -20.0f), b2Vec2(-20.0f, 20.0f));
            ground->CreateFixture(&sd);

            // Right vertical
            shape.SetTwoSided(b2Vec2(20.0f, -20.0f), b2Vec2(20.0f, 20.0f));
            ground->CreateFixture(&sd);

            // Top horizontal
            shape.SetTwoSided(b2Vec2(-20.0f, 20.0f), b2Vec2(20.0f, 20.0f));
            ground->CreateFixture(&sd);

            // Bottom horizontal
            shape.SetTwoSided(b2Vec2(-20.0f, -20.0f), b2Vec2(20.0f, -20.0f));
            ground->CreateFixture(&sd);
        }

        {
            b2Transform xf1;
            xf1.q.Set(0.3524f * b2_pi);
            xf1.p = xf1.q.GetXAxis();

            b2Vec2 vertices[3];
            vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f));
            vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f));
            vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f));

            b2PolygonShape poly1;
            poly1.Set(vertices, 3);

            b2FixtureDef sd1;
            sd1.shape   = &poly1;
            sd1.density = 2.0f;

            b2Transform xf2;
            xf2.q.Set(-0.3524f * b2_pi);
            xf2.p = -xf2.q.GetXAxis();

            vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f));
            vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f));
            vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f));

            b2PolygonShape poly2;
            poly2.Set(vertices, 3);

            b2FixtureDef sd2;
            sd2.shape   = &poly2;
            sd2.density = 2.0f;

            b2BodyDef bd;
            bd.type = b2_dynamicBody;

            bd.position.Set(0.0f, 3.0);
            bd.angle      = b2_pi;
            bd.allowSleep = false;
            m_body        = m_world->CreateBody(&bd);
            m_body->CreateFixture(&sd1);
            m_body->CreateFixture(&sd2);

            float gravity = 10.0f;
            float I       = m_body->GetInertia();
            float mass    = m_body->GetMass();

            // Compute an effective radius that can be used to
            // set the max torque for a friction joint
            // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
            float radius = b2Sqrt(2.0f * I / mass);

            b2FrictionJointDef jd;
            jd.bodyA = ground;
            jd.bodyB = m_body;
            jd.localAnchorA.SetZero();
            jd.localAnchorB     = m_body->GetLocalCenter();
            jd.collideConnected = true;
            jd.maxForce         = 0.5f * mass * gravity;
            jd.maxTorque        = 0.2f * mass * radius * gravity;

            m_world->CreateJoint(&jd);
        }

        {
            b2PolygonShape shape;
            shape.SetAsBox(0.5f, 0.5f);

            b2FixtureDef fd;
            fd.shape    = &shape;
            fd.density  = 1.0f;
            fd.friction = 0.3f;

            for (int i = 0; i < 10; ++i)
            {
                b2BodyDef bd;
                bd.type = b2_dynamicBody;

                bd.position.Set(0.0f, 7.0f + 1.54f * i);
                b2Body* body = m_world->CreateBody(&bd);

                body->CreateFixture(&fd);

                float gravity = 10.0f;
                float I       = body->GetInertia();
                float mass    = body->GetMass();

                // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
                float radius = b2Sqrt(2.0f * I / mass);

                b2FrictionJointDef jd;
                jd.localAnchorA.SetZero();
                jd.localAnchorB.SetZero();
                jd.bodyA            = ground;
                jd.bodyB            = body;
                jd.collideConnected = true;
                jd.maxForce         = mass * gravity;
                jd.maxTorque        = 0.1f * mass * radius * gravity;

                m_world->CreateJoint(&jd);
            }
        }
    }

    void Step(Settings& settings) override
    {
        DrawString(5, m_textLine, "Forward (W), Turn (A) and (D)");

        // if (glfwGetKey(g_mainWindow, GLFW_KEY_W) == GLFW_PRESS)
        //{
        //	b2Vec2 f = m_body->GetWorldVector(b2Vec2(0.0f, -50.0f));
        //	b2Vec2 p = m_body->GetWorldPoint(b2Vec2(0.0f, 3.0f));
        //	m_body->ApplyForce(f, p, true);
        // }

        // if (glfwGetKey(g_mainWindow, GLFW_KEY_A) == GLFW_PRESS)
        //{
        //	m_body->ApplyTorque(10.0f, true);
        // }

        // if (glfwGetKey(g_mainWindow, GLFW_KEY_D) == GLFW_PRESS)
        //{
        //	m_body->ApplyTorque(-10.0f, true);
        // }

        Test::Step(settings);
    }

    static Test* Create() { return new ApplyForce; }

    b2Body* m_body;
};

static int testIndex = RegisterTest("Forces", "Apply Force", ApplyForce::Create);
